<!doctype html>
<html lang="zh-CN">
<head>
  <base href="https://www.nodeapp.cn/inspector.html" />
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>检查工具（Inspector） | Node.js 中文文档 | Node.js 中文网</title>
  <meta name="description" content="Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。Node.js 的包管理器 npm，是全球最大的开源库生态系统。">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://www.nodeapp.cn/inspector.html">
  <link rel="apple-touch-icon" href="apple-touch-icon.png">
  <link rel="icon" sizes="32x32" type="image/png" href="favicon.png">
  
  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?acdf78480f7f8f2b23b812565a5868e0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>
<body class="alt apidoc" id="api-section-inspector">
  <div id="content" class="clearfix">
    <div id="column1" data-id="inspector" class="interior">
      <header>
        <h1>Node.js v8.x 中文文档</h1>
        <hr>
      </header>

      <div id="toc">
        <h2>目录</h2>
        <ul>
<li><span class="stability_1"><a href="#inspector_inspector">Inspector</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_inspector_open_port_host_wait">inspector.open([port[, host[, wait]]])</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_inspector_close">inspector.close()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_inspector_url">inspector.url()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#inspector_class_inspector_session">Class: inspector.Session</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_constructor_new_inspector_session">Constructor: new inspector.Session()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_event_inspectornotification">Event: &apos;inspectorNotification&apos;</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_event_lt_inspector_protocol_method_gt">Event: &lt;inspector-protocol-method&gt;</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_connect">session.connect()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_post_method_params_callback">session.post(method[, params][, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_disconnect">session.disconnect()</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>
<div id="apicontent">
        <h1>Inspector<span><a class="mark" href="#inspector_inspector" id="inspector_inspector">#</a></span></h1>
<!--introduced_in=v8.0.0-->
<div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p>The <code>inspector</code> module provides an API for interacting with the V8 inspector.</p>
<p>It can be accessed using:</p>
<pre><code class="lang-js">const inspector = require(&apos;inspector&apos;);
</code></pre>
<h2>inspector.open([port[, host[, wait]]])<span><a class="mark" href="#inspector_inspector_open_port_host_wait" id="inspector_inspector_open_port_host_wait">#</a></span></h2>
<div class="signature"><ul>
<li>port 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Port to listen on for inspector connections. Optional,
defaults to what was specified on the CLI.</li>
<li>host 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Host to listen on for inspector connections. Optional,
defaults to what was specified on the CLI.</li>
<li>wait 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> Block until a client has connected. Optional, defaults
to false.</li>
</ul>
</div><p>Activate inspector on host and port. Equivalent to <code>node
--inspect=[[host:]port]</code>, but can be done programmatically after node has
started.</p>
<p>If wait is <code>true</code>, will block until a client has connected to the inspect port
and flow control has been passed to the debugger client.</p>
<h3>inspector.close()<span><a class="mark" href="#inspector_inspector_close" id="inspector_inspector_close">#</a></span></h3>
<p>Deactivate the inspector. Blocks until there are no active connections.</p>
<h3>inspector.url()<span><a class="mark" href="#inspector_inspector_url" id="inspector_inspector_url">#</a></span></h3>
<p>Return the URL of the active inspector, or <code>undefined</code> if there is none.</p>
<h2>Class: inspector.Session<span><a class="mark" href="#inspector_class_inspector_session" id="inspector_class_inspector_session">#</a></span></h2>
<p>The <code>inspector.Session</code> is used for dispatching messages to the V8 inspector
back-end and receiving message responses and notifications.</p>
<h3>Constructor: new inspector.Session()<span><a class="mark" href="#inspector_constructor_new_inspector_session" id="inspector_constructor_new_inspector_session">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><p>Create a new instance of the <code>inspector.Session</code> class. The inspector session
needs to be connected through <a href="#inspector_session_connect"><code>session.connect()</code></a> before the messages
can be dispatched to the inspector backend.</p>
<p><code>inspector.Session</code> is an <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: &apos;inspectorNotification&apos;<span><a class="mark" href="#inspector_event_inspectornotification" id="inspector_event_inspectornotification">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><ul>
<li>
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> The notification message object</li>
</ul>
<p>Emitted when any notification from the V8 Inspector is received.</p>
<pre><code class="lang-js">session.on(&apos;inspectorNotification&apos;, (message) =&gt; console.log(message.method));
// Debugger.paused
// Debugger.resumed
</code></pre>
<p>It is also possible to subscribe only to notifications with specific method:</p>
<h3>Event: &lt;inspector-protocol-method&gt;<span><a class="mark" href="#inspector_event_lt_inspector_protocol_method_gt" id="inspector_event_lt_inspector_protocol_method_gt">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><ul>
<li>
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> The notification message object</li>
</ul>
<p>Emitted when an inspector notification is received that has its method field set
to the <code>&lt;inspector-protocol-method&gt;</code> value.</p>
<p>The following snippet installs a listener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger/#event-paused"><code>Debugger.paused</code></a>
event, and prints the reason for program suspension whenever program
execution is suspended (through breakpoints, for example):</p>
<pre><code class="lang-js">session.on(&apos;Debugger.paused&apos;, ({ params }) =&gt; {
  console.log(params.hitBreakpoints);
});
// [ &apos;/the/file/that/has/the/breakpoint.js:11:0&apos; ]
</code></pre>
<h3>session.connect()<span><a class="mark" href="#inspector_session_connect" id="inspector_session_connect">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><p>Connects a session to the inspector back-end. An exception will be thrown
if there is already a connected session established either through the API or by
a front-end connected to the Inspector WebSocket port.</p>
<h3>session.post(method[, params][, callback])<span><a class="mark" href="#inspector_session_post_method_params_callback" id="inspector_session_post_method_params_callback">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><ul>
<li>method 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li>params 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></li>
<li>callback 
            <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a></li>
</ul>
<p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
a response is received. <code>callback</code> is a function that accepts two optional
arguments - error and message-specific result.</p>
<pre><code class="lang-js">session.post(&apos;Runtime.evaluate&apos;, { expression: &apos;2 + 2&apos; },
             (error, { result }) =&gt; console.log(result));
// Output: { type: &apos;number&apos;, value: 4, description: &apos;4&apos; }
</code></pre>
<p>The latest version of the V8 inspector protocol is published on the
<a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a>.</p>
<p>Node inspector supports all the Chrome DevTools Protocol domains declared
by V8. Chrome DevTools Protocol domain provides an interface for interacting
with one of the runtime agents used to inspect the application state and listen
to the run-time events.</p>
<h3>session.disconnect()<span><a class="mark" href="#inspector_session_disconnect" id="inspector_session_disconnect">#</a></span></h3>
<div class="api_metadata">
<span>新增于: v8.0.0</span>
</div><p>Immediately close the session. All pending message callbacks will be called
with an error. <a href="#inspector_session_connect"><code>session.connect()</code></a> will need to be called to be able to send
messages again. Reconnected session will lose all inspector state, such as
enabled agents or configured breakpoints.</p>

      </div>
    </div>

    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js 中文文档 | Node.js 中文网
        </a>
      </div>
      
        <!-- [start-include:_toc.md] -->
<ul>
<li><a href="documentation.html">关于本文档</a></li>
<li><a href="synopsis.html">用法与例子</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a href="assert.html">断言测试</a></li>
<li><a href="async_hooks.html">异步钩子（Async Hooks）</a></li>
<li><a href="buffer.html">缓存（Buffer）</a></li>
<li><a href="addons.html">C++ 插件</a></li>
<li><a href="n-api.html">C/C++ 插件 - N-API</a></li>
<li><a href="child_process.html">子进程</a></li>
<li><a href="cluster.html">集群（Cluster）</a></li>
<li><a href="cli.html">命令行参数</a></li>
<li><a href="console.html">控制台（Console）</a></li>
<li><a href="crypto.html">加密（Crypto）</a></li>
<li><a href="debugger.html">调试器</a></li>
<li><a href="deprecations.html">废弃的 API</a></li>
<li><a href="dns.html">DNS</a></li>
<li><a href="domain.html">域（Domain）</a></li>
<li><a href="esm.html">ECMAScript 模块</a></li>
<li><a href="errors.html">错误（Errors）</a></li>
<li><a href="events.html">事件（Events）</a></li>
<li><a href="fs.html">文件系统</a></li>
<li><a href="globals.html">全局对象（Globals）</a></li>
<li><a href="http.html">HTTP</a></li>
<li><a href="http2.html">HTTP/2</a></li>
<li><a href="https.html">HTTPS</a></li>
<li><a href="inspector.html">检查工具（Inspector）</a></li>
<li><a href="intl.html">国际化</a></li>
<li><a href="modules.html">模块（Modules）</a></li>
<li><a href="net.html">网络（Net）</a></li>
<li><a href="os.html">操作系统（OS）</a></li>
<li><a href="path.html">路径（Path）</a></li>
<li><a href="perf_hooks.html">性能钩子（Performance Hooks）</a></li>
<li><a href="process.html">进程</a></li>
<li><a href="punycode.html">Punycode</a></li>
<li><a href="querystring.html">查询字符串</a></li>
<li><a href="readline.html">逐行读取</a></li>
<li><a href="repl.html">交互式解释器（REPL）</a></li>
<li><a href="stream.html">流（Stream）</a></li>
<li><a href="string_decoder.html">字符串解码</a></li>
<li><a href="timers.html">定时器（Timers）</a></li>
<li><a href="tls.html">安全传输层（TLS/SSL）</a></li>
<li><a href="tracing.html">事件跟踪（Tracing）</a></li>
<li><a href="tty.html">TTY</a></li>
<li><a href="dgram.html">UDP / 数据报</a></li>
<li><a href="url.html">URL</a></li>
<li><a href="util.html">工具集</a></li>
<li><a href="v8.html">V8</a></li>
<li><a href="vm.html">虚拟机（VM）</a></li>
<li><a href="zlib.html">压缩（ZLIB）</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a href="https://github.com/nodejs/node">GitHub 仓库和问题跟踪</a></li>
<li><a href="https://groups.google.com/group/nodejs">邮件列表</a></li>
</ul>
<!-- [end-include:_toc.md] -->

      
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
</body>
</html>
